package dparish.client.common.widgets;

import java.util.Iterator;

import com.google.gwt.dom.client.Element;
import com.google.gwt.uibinder.client.UiChild;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class LabeledValueWidget extends Composite implements HasWidgets{
		
		private CaptionPanel captionPanel = new CaptionPanel();
		private FlowPanel container = new FlowPanel(); // the real adding goes here since captionPanel can only hold one widget 
		private HTMLPanel dlPanel = null;
		private Resources resources;
		
		private String labelStyle = null;
		private String valueStyle = null;

		Element dlElement; // The parent <DL> element of the widget. Used to add the left and right rows.
		
		static boolean isCssInjected = false;
		
		// We use this to add assign the value widget to this label widget.  If null, than we've added a value widget and the next is a label widget.
		private Widget lastLabelWidget = null;
		
		public LabeledValueWidget () {
			this.initWidget(captionPanel);
			captionPanel.add(container);
			String id = HTMLPanel.createUniqueId();
			dlPanel = new HTMLPanel ("<dl id=\"" + id + "\"></dl>");
			dlElement = dlPanel.getElementById(id);

			container.clear();
			dlPanel.clear();
			container.add(dlPanel);
			
			resources = Resources.INSTANCE;
			if (!isCssInjected) {
				isCssInjected = resources.css().ensureInjected();
			}
		}
		
		
		public void setStyles (String labelStyle, String valueStyle) {
			this.labelStyle = labelStyle;
			this.valueStyle = valueStyle;
			
		}
		
		public void setCaption (String caption) {
			captionPanel.setCaptionText(caption);
		}

		
@UiChild
public void addLeft (Widget w) {
	lastLabelWidget = w;
}

@UiChild
public void addRight (Widget w) {
	fillRow (lastLabelWidget, w);
}

@UiChild
public void addNormal (Widget w) {
	container.add(w);
}
		
		/**
		 * Called when we have received a full row. This attaches the label and value as dt and dd elements
		 * 
		 * @param labelWidget The left widget (traditionally label)
		 * @param valueWidget The right widget
		 */
		private void fillRow (Widget labelWidget, Widget valueWidget) {
			Element dt = dt();
			dlElement.appendChild(dt);
			dlPanel.add(labelWidget, dt);
			Element dd = dd();
			dlElement.appendChild(dd);
			dlPanel.add(valueWidget, dd);
		}
		

		/**
		 * Create the dt element with the proper styles
		 * 
		 */
		private Element dt() {
			Element e = DOM.createElement("dt");
			e.addClassName(resources.css().label());
			if (labelStyle != null) {
				e.setClassName(labelStyle);
			}
			return e;
		}

		/**
		 * Create the dd element with the proper styles
		 */
		private Element dd() {
			Element e = DOM.createElement("dd");
			e.addClassName(resources.css().value());
			if (valueStyle != null) {
				e.setClassName(valueStyle);
			}
			return e;
		}
		

		@Override
		public void clear() {
			lastLabelWidget = null;
			container.clear();
		}


		@Override
		public Iterator<Widget> iterator() {
			return null;
		}


		@Override
		public boolean remove(Widget w) {
			return false;
		}


		@Override
		public void add(Widget w) {
			addNormal(w);
		}
		
}
